using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace cs2335.lab5
{
    public class ProgressBar: IComponent
    {

        #region Variables
        protected Rectangle container = new Rectangle(0, 0, 100, 25);
        protected StringFormat labelFormat = new StringFormat(StringFormatFlags.NoClip);
        protected SolidBrush backColor = new SolidBrush(Color.FloralWhite);
        protected Font font = new Font("SansSerif", 10);
        protected CCTForm owner;
        protected bool hasFocus = false;
        protected bool hidden = false;
        protected string name;
        protected string text;
        
        protected SolidBrush barColor = new SolidBrush(Color.Salmon);
        protected int step;
        protected int max;
        protected int min;
        protected int val;
        #endregion

        #region Properties
        /// <summary>
        /// Tells whether or not a component is hidden. If it is hidden,
        /// it should not be receiving actions.
        /// </summary>
        public bool Hidden
        {
            get { return hidden; }
        }

        /// <summary>
        /// Stores the width and height of the component
        /// </summary>
        public Size Size
        {
            get { return container.Size; }
            set { container.Size = value; Invalidate(); }
        }

        public Point Location
        {
            get { return container.Location; }
            set
            {
                container.Location = value;
                Invalidate();
            }
        }

        public Rectangle Bounds
        {
            get { return container; }
            set
            {
                container = value;
                Invalidate();
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public System.Drawing.Font Font
        {
            get { return font; }
            set { font = value; Invalidate(); }
        }

        public string Text
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }

        public CCTForm Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public Color BackColor
        {
            get { return backColor.Color; }
            set { backColor.Color = value; Invalidate(); }
        }
        #endregion

        #region Events
        public event EventHandler Click;
        public event EventHandler GotFocus;
        public event EventHandler KeyPress;
        public event EventHandler KeyDown;
        public event EventHandler LostFocus;
        public event EventHandler Paint;
        #endregion

        #region IComponent Members
        public void Invalidate()
        {
            if (owner != null)
            {
                owner.Invalidate();
            }
        }

        public void Hide()
        {
            hidden = true;
            Invalidate();
        }

        public void Show()
        {
            hidden = false;
            Invalidate();
        }


        public void OnPaint(PaintEventArgs e)
        {
            if (!hidden)
            {
                Graphics g = e.Graphics;
                g.FillRectangle(backColor, container);

                Draw3DBorder(g, container);
                DrawProgressBar(g);

                if (hasFocus)
                {
                    DrawFocusBorder(g);
                }

                if (Paint != null)
                {
                    Paint(this, e);
                }
            }

        }

        public void OnGotFocus(EventArgs e)
        {
            hasFocus = true;
            if (GotFocus != null)
            {
                GotFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnLostFocus(EventArgs e)
        {
            hasFocus = false;
            if (LostFocus != null)
            {
                LostFocus(this, e);
            }
            owner.Invalidate();
        }


        public void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        public void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            //Invokes a Click event if the user hits ENTER
            //  while this component has focus
            if (e.KeyChar == '\r')
            {
                if (Click != null)
                {
                    Click(this, e);
                }
            }

            if (KeyPress != null)
            {
                KeyPress(this, e);
            }
        }

        public void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (KeyDown != null)
            {
                KeyDown(this, e);
            }
        }
        #endregion

        public ProgressBar() 
        {
            step = 0;
            max = 100;
            min = 0;
            val = 0;
        }

        public int Value
        {
            get { return val; }
            set 
            {
                if (value < min)
                {
                    val = min;
                }
                else if (value > max)
                {
                    val = max;
                }
                else
                {
                    val = value;
                }
                Invalidate();
            }
        }

        public int Step
        {
            get { return step; }
            set { step = value; }
        }

        public int Maximum
        {
            get { return max; }
            set 
            {
                if (value < min)
                {
                    min = value;
                }
                max = value;
                if (val > max)
                {
                    val = max;
                }
                Invalidate();
            }
        }

        public int Minumum
        {
            get { return min; }
            set 
            {
                if (value > max)
                {
                    max = value;
                    min = value;
                }
                else if (value < 0)
                {
                    min = 0;
                }
                else
                {
                    min = value;
                }
                if (val < min)
                {
                    val = min;
                }
                Invalidate();
            }
        }

        public SolidBrush BarColor
        {
            get { return barColor; }
            set { barColor = value; }
        }

        public void PerformStep()
        {
            if (val + step < max)
            {
                val += + step;
            }
            else if (val + step >= max)
            {
                val = max;
            }
            Invalidate();
        }

        private void DrawFocusBorder(Graphics g)
        {
            int PenWidth = (int)Pens.White.Width;

            g.DrawLine(Pens.DarkGoldenrod, new Point(container.Left, container.Top),
                new Point(container.Right - PenWidth, container.Top));
            g.DrawLine(Pens.DarkGoldenrod, new Point(container.Left, container.Top),
                new Point(container.Left, container.Bottom));
            g.DrawLine(Pens.PaleGoldenrod, new Point(container.Left, container.Bottom),
                new Point(container.Right - PenWidth, container.Bottom));
            g.DrawLine(Pens.PaleGoldenrod, new Point(container.Right - PenWidth, container.Bottom),
                new Point(container.Right - PenWidth, container.Top));
        }

        private void Draw3DBorder(Graphics g, Rectangle r)
        {
            int PenWidth = (int)Pens.White.Width;

            g.DrawLine(Pens.PaleGoldenrod, new Point(r.Left, r.Top),
                new Point(r.Right - PenWidth, r.Top));
            g.DrawLine(Pens.PaleGoldenrod, new Point(r.Left, container.Top),
                new Point(r.Left, r.Bottom));
            g.DrawLine(Pens.DarkGoldenrod, new Point(r.Left, r.Bottom),
                new Point(r.Right - PenWidth, r.Bottom));
            g.DrawLine(Pens.DarkGoldenrod, new Point(container.Right - PenWidth, r.Bottom),
                new Point(r.Right - PenWidth, r.Top));
        }

        private void DrawProgressBar(Graphics g)
        {
            Pen pen = Pens.PaleGoldenrod;
            Rectangle tempBar = container;
            tempBar.X += (int)pen.Width;
            tempBar.Y += (int)pen.Width;
            float percent = (float)(val - min) / (float)(max - min);
            tempBar.Width = (int)((float)(percent * (container.Width - pen.Width)));
            tempBar.Height -= (int)pen.Width;
            g.FillRectangle(barColor, tempBar);
        }
    }
}
